package com.cake.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cake.dao.XxAreaMapper;
import com.cake.entity.XxArea;
import com.cake.entity.XxOrder;
import com.cake.param.ThermodynamicParam;
import com.cake.service.base.BaseService;
import com.cake.service.inter.IThermodynamicService;

/**
 * 热力图业务层
 * @author Administrator
 *
 */
@Service
@Transactional
public class ThermodynamicServiceImpl extends BaseService<XxArea> implements IThermodynamicService{

	@Autowired
	private XxAreaMapper xxAreaMapper;

	@Override
	public List<Object> selectReceiverTotalByDistrictOrCityOrProvince(
			ThermodynamicParam thermodynamicParam) {
		List<Object> resultList = new ArrayList<Object>();
		List<String> nameList = new ArrayList<String>();
		List<Integer> totalList = new ArrayList<Integer>();
		/**
		 *	省份收货人数量热力图
		 */
		if(thermodynamicParam.getAreaType().equals(1)){
			List<XxArea> provinces = xxAreaMapper.selectProvince();
			Map<String, Object> selectMap = new HashMap<String, Object>();
			for (XxArea xxArea : provinces) {
				selectMap.put("provinceCode", xxArea.getCode());
				Integer receiverTotal = xxAreaMapper.selectReceiverTotalByDistrictOrCityOrProvince(selectMap);
				if(receiverTotal > 0){
					nameList.add(xxArea.getName());
					totalList.add(receiverTotal);
				}
			}
			/**
			 * 城市收货人数量热力图
			 */
		}else if(thermodynamicParam.getAreaType().equals(2)){
			List<XxArea> citys = xxAreaMapper.selectCityByProvinceCode(thermodynamicParam.getAreaCode());
			Map<String, Object> selectMap = new HashMap<String, Object>();
			for (XxArea xxArea : citys) {
				selectMap.put("cityCode", xxArea.getCode());
				Integer receiverTotal = xxAreaMapper.selectReceiverTotalByDistrictOrCityOrProvince(selectMap);
				if(receiverTotal > 0){
					nameList.add(xxArea.getName());
					totalList.add(receiverTotal);
				}
			}
			/**
			 *	区县收货人数量热力图
			 */
		}else if(thermodynamicParam.getAreaType().equals(3)){
			List<XxArea> districts = xxAreaMapper.selectDistrictByCityCode(thermodynamicParam.getAreaCode());
			Map<String, Object> selectMap = new HashMap<String, Object>();
			for (XxArea xxArea : districts) {
				selectMap.put("districtCode", xxArea.getCode());
				Integer receiverTotal = xxAreaMapper.selectReceiverTotalByDistrictOrCityOrProvince(selectMap);
				if(receiverTotal > 0){
					nameList.add(xxArea.getName());
					totalList.add(receiverTotal);
				}
			}
		}
		resultList.add(nameList);
		resultList.add(totalList);
		return resultList;
	}

	@Override
	public List<Object> selectOrderTotalByDistrictOrCityOrProvince(ThermodynamicParam thermodynamicParam) {
		List<Object> resultList = new ArrayList<Object>();
		List<String> nameList = new ArrayList<String>();
		List<Integer> totalList = new ArrayList<Integer>();
		/**
		 * 省份订单数量热力图
		 */
		if(thermodynamicParam.getAreaType().equals(1)){
			List<XxArea> provinces = xxAreaMapper.selectProvince();
			Map<String, Object> selectMap = new HashMap<String, Object>();
			for (XxArea xxArea : provinces) {
				selectMap.put("provinceCode", xxArea.getCode());
				selectMap.put("startTime", thermodynamicParam.getStartTime());
				selectMap.put("endTime", thermodynamicParam.getEndTime());
				Integer receiverTotal = xxAreaMapper.selectOrderTotalByDistrictOrCityOrProvince(selectMap);
				if(receiverTotal > 0){
					nameList.add(xxArea.getName());
					totalList.add(receiverTotal);
				}
			}
			/**
			 * 城市订单数热力图
			 */
		}else if(thermodynamicParam.getAreaType().equals(2)){
			List<XxArea> citys = xxAreaMapper.selectCityByProvinceCode(thermodynamicParam.getAreaCode());
			Map<String, Object> selectMap = new HashMap<String, Object>();
			for (XxArea xxArea : citys) {
				selectMap.put("cityCode", xxArea.getCode());
				selectMap.put("startTime", thermodynamicParam.getStartTime());
				selectMap.put("endTime", thermodynamicParam.getEndTime());
				Integer receiverTotal = xxAreaMapper.selectOrderTotalByDistrictOrCityOrProvince(selectMap);
				if(receiverTotal > 0){
					nameList.add(xxArea.getName());
					totalList.add(receiverTotal);
				}
			}
			/**
			 * 区县订单数热力图
			 */
		}else if(thermodynamicParam.getAreaType().equals(3)){
			List<XxArea> districts = xxAreaMapper.selectDistrictByCityCode(thermodynamicParam.getAreaCode());
			Map<String, Object> selectMap = new HashMap<String, Object>();
			for (XxArea xxArea : districts) {
				selectMap.put("districtCode", xxArea.getCode());
				selectMap.put("startTime", thermodynamicParam.getStartTime());
				selectMap.put("endTime", thermodynamicParam.getEndTime());
				Integer receiverTotal = xxAreaMapper.selectOrderTotalByDistrictOrCityOrProvince(selectMap);
				if(receiverTotal > 0){
					nameList.add(xxArea.getName());
					totalList.add(receiverTotal);
				}
			}
		}
		resultList.add(nameList);
		resultList.add(totalList);
		return resultList;
	}

	@Override
	public List<Object> selectAmountByDistrictOrCityOrProvince(ThermodynamicParam thermodynamicParam) {
		List<Object> resultList = new ArrayList<Object>();
		List<String> nameList = new ArrayList<String>();
		List<BigDecimal> totalList = new ArrayList<BigDecimal>();
		/**
		 * 省份销售总额热力图
		 */
		if(thermodynamicParam.getAreaType().equals(1)){
			List<XxArea> list = xxAreaMapper.selectProvince();
			Map<String, Object> selectMap = new HashMap<String, Object>();
			for (XxArea xxArea : list) {
				selectMap.put("provinceCode", xxArea.getCode());
				selectMap.put("startTime", thermodynamicParam.getStartTime());
				selectMap.put("endTime", thermodynamicParam.getEndTime());
				List<XxOrder> orders = xxAreaMapper.selectOrderItemsByDistrictOrCityOrProvince(selectMap);
				if(orders.size() > 0){
					BigDecimal totalPrice = new BigDecimal(0);
					for (XxOrder xxOrder : orders) {
						totalPrice = totalPrice.add(xxOrder.getTotalAmount());
					}
					nameList.add(xxArea.getName());
					totalList.add(totalPrice);
				}
			}
			/**
			 * 城市销售总额热力图
			 */
		}else if(thermodynamicParam.getAreaType().equals(2)){
			List<XxArea> list = xxAreaMapper.selectCityByProvinceCode(thermodynamicParam.getAreaCode());
			Map<String, Object> selectMap = new HashMap<String, Object>();
			for (XxArea xxArea : list) {
				selectMap.put("cityCode", xxArea.getCode());
				selectMap.put("startTime", thermodynamicParam.getStartTime());
				selectMap.put("endTime", thermodynamicParam.getEndTime());
				List<XxOrder> orders = xxAreaMapper.selectOrderItemsByDistrictOrCityOrProvince(selectMap);
				if(orders.size() > 0){
					BigDecimal totalPrice = new BigDecimal(0);
					for (XxOrder xxOrder : orders) {
						totalPrice = totalPrice.add(xxOrder.getTotalAmount());
					}
					nameList.add(xxArea.getName());
					totalList.add(totalPrice);
				}
			}
			/**
			 * 区县销售额热力图
			 */
		}else if(thermodynamicParam.getAreaType().equals(3)){
			
			List<XxArea> list = xxAreaMapper.selectDistrictByCityCode(thermodynamicParam.getAreaCode());
			Map<String, Object> selectMap = new HashMap<String, Object>();
			for (XxArea xxArea : list) {
				selectMap.put("districtCode", xxArea.getCode());
				selectMap.put("startTime", thermodynamicParam.getStartTime());
				selectMap.put("endTime", thermodynamicParam.getEndTime());
				List<XxOrder> orders = xxAreaMapper.selectOrderItemsByDistrictOrCityOrProvince(selectMap);
				if(orders.size() > 0){
					BigDecimal totalPrice = new BigDecimal(0);
					for (XxOrder xxOrder : orders) {
						totalPrice = totalPrice.add(xxOrder.getTotalAmount());
					}
					nameList.add(xxArea.getName());
					totalList.add(totalPrice);
				}
			}
		}
		resultList.add(nameList);
		resultList.add(totalList);
		return resultList;
	}
	
}
